import { Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, Query, Req } from "@nestjs/common";
import { ApiBearerAuth, ApiOperation, ApiTags } from "@nestjs/swagger";
import { Request } from "express";

import { SkipJwtAuth } from "@/core/auth/decorator/skip-auth.decorator";
import { ApiBaseResponse } from "@/core/decorators/api-base-response.decorator";
import { ApiPageResponse } from "@/core/decorators/api-page-response.decorator";
import AjaxResult from "@/core/utils/AjaxResult";

import { CommentsService } from "./comments.service";
import { CreateCommentDto } from "./dto/create-comment.dto";
import { QueryCommentsDto } from "./dto/query-comments.dto";
import { UpdateCommentDto } from "./dto/update-comment.dto";
import { CommentEntity } from "./entities/comment.entity";

@ApiTags("评论管理")
@ApiBearerAuth()
@Controller("/admin/comments")
export class CommentsAdminController {
    constructor(private readonly commentsService: CommentsService) {}

    @ApiOperation({ summary: "评论添加" })
    @ApiBaseResponse(CommentEntity)
    @Post("/add")
    async create(@Body() createCommentDto: CreateCommentDto, @Req() req: Request) {
        return AjaxResult.success(await this.commentsService.create(createCommentDto, req.user.id));
    }

    @SkipJwtAuth()
    @ApiOperation({ summary: "评论列表" })
    @ApiBaseResponse({ type: "array", items: CommentEntity })
    @Get("/list")
    async list() {
        return AjaxResult.success(await this.commentsService.findAll());
    }

    @SkipJwtAuth()
    @ApiOperation({ summary: "评论分页" })
    @ApiPageResponse(CommentEntity)
    @Get("/page")
    async page(@Query() query: QueryCommentsDto) {
        return AjaxResult.success(await this.commentsService.findPage(query));
    }

    @SkipJwtAuth()
    @ApiOperation({ summary: "评论详情" })
    @ApiBaseResponse(CommentEntity)
    @Get("/detail/:id")
    async findOne(@Param("id", ParseIntPipe) id: number) {
        return AjaxResult.success(await this.commentsService.findOne(id));
    }

    @ApiOperation({ summary: "评论修改" })
    @ApiBaseResponse(CommentEntity)
    @Patch("/edit/:id")
    async update(@Param("id", ParseIntPipe) id: number, @Body() updateCommentDto: UpdateCommentDto) {
        return AjaxResult.success(await this.commentsService.update(id, updateCommentDto));
    }

    @ApiOperation({ summary: "评论删除" })
    @Delete("/delete/:id")
    async remove(@Param("id", ParseIntPipe) id: number) {
        return AjaxResult.success(await this.commentsService.remove(id));
    }
}
